<?php
/**
 * Created by PhpStorm.
 * User: edmmss
 * Date: 2018/11/14
 * Time: 09:57
 */

namespace app\modules\prototype\controller;

use app\modules\prototype\creational\BarBookPrototype;
use app\modules\prototype\creational\FooBookPrototype;
use yii\web\Controller;

class DefaultController extends Controller
{
    /**
     * 原型模式示例
     * 相比正常创建一个对象，首先创建一个原型，然后克隆它会更节省开销
     *
     * @date   2018/11/14
     * @author edmmss
     * @return bool
     */
    public function actionIndex()
    {
        $fooPrototype = new FooBookPrototype();
        $barPrototype = new BarBookPrototype();

        $bookPool = [];
        for ($i = 0; $i < 3; $i++) {
            $book = clone $fooPrototype;
            $book->setTitle('Foo Book No' . $i);
            $bookPool[] = $book;
        }

        var_dump($bookPool);
        var_dump($bookPool[0] === $bookPool[1]);

        return true;
    }
}